package com.lanhao.aot.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.dictionary.CustomDictionary;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;

public class AddressExtractionExample {
	/***
	 * 
	 * hanlp分词
	 * 
	 * @param input
	 * 
	 */

	public static Map<String, String> getOut(String input) {

		Map<String, String> resMap = new HashMap<>();

		// 使用Hanlp分词;允许用户自定义词性字典

		Segment segment = HanLP.newSegment().enableCustomDictionary(true);

		CustomDictionary.add("安徽", "province");
		CustomDictionary.add("山东", "province");
		CustomDictionary.add("广东", "province");

		CustomDictionary.add("合肥", "city");
		CustomDictionary.add("六安", "city");
		CustomDictionary.add("广州", "city");

		List<Term> termList = segment.seg(input);

		// 遍历分词结果
		for (Term term : termList) {
			String word = term.toString().substring(0, term.length()); // 词
			String nature = term.toString().substring(term.length() + 1); // 词性
			if (StringUtils.isNotBlank(word) && StringUtils.isNotBlank(nature)) {
				// 将词及词性放到Map结果集中
				resMap.put(word, nature);
			}
		}
		return resMap;
	}

	public static void main(String[] args) {
		Map<String, String> out = getOut("我要在安徽合肥金安区高速时代御府装个300M的宽带");
		System.out.println("out = " + out);

	}
}
